<template>
    <!--<span class="el-button el-button&#45;&#45;text file-input-button">
        <span>上传</span>
        <input accept="image/*" type="file" @change="uploadFile($event)">
    </span>-->
    <div>
        <el-button type="text" @click="uploadFile" v-if="readyShow">上传</el-button>
        <input type="hidden" id="uploadText3" value="-1"/>
    </div>
</template>

<script>
    import axios from 'axios';
    import util from 'assets/js/util.js'
    import conf from 'assets/js/conf.js'

    export default {
        data() {
            return {
                _file: null,
                readyShow: false,
            }
        },
        mounted() {
            const _self = this;
            axios.get('http://web.zj.icbc.com.cn/bankruptcy/v1/mobile/pic/getParam?url=' + encodeURIComponent(location.href.split('#')[0]))
                .then((res) => {
                    if (res.data.retCode === '00000') {
                        rel.init({
                            appid: res.data.appid,
                            timestamp: res.data.timestamp,
                            nonceStr: res.data.nonceStr,
                            signature: res.data.signature
                        });
                        rel.ready(function () {
                            _self.readyShow = true;
                        });
                    }
                });
            setTimeout(function () {
                _self.query();
            }, 1000);
        },
        methods: {
            uploadFile: function () {
                rel.chooseImage({
                    flag: "0",
                    success: function (res) {
                        rel.uploadImage({
                            localIds: res.retMsg.localIds,
                            success: function (res) {

                            },
                            complete: function (res) {
                                document.getElementById("uploadText3").value = res.retMsg.url
                            }
                        });
                    },
                });
//                });
                /*util.upload({
                        success: function (url) {
                            let form = new FormData();
                            form.append('url', url);
                            util.ajaxByForm({
                                url: `/file/uploadByIcbcUrl/` + util.getUrlParam(document.location.href, 'caseId'),
                                form: form,
                                success: function (data) {
                                    _self.$emit('getFileList', data.message)
                                },
                                error: function (err) {

                                }
                            });
                        },
                        complete: function () {
                            _self.test = 2;
                        },
                        cancel: function () {
                            _self.test = 3;
                        },
                        failed: function () {
                            _self.test = 4;
                        }
                    }
                );*/

                /*const _self = this;
                for (let i = 0; i < event.target.files.length; i++) {
                    let form = new FormData();
                    form.append('file', event.target.files[i]);
                    util.ajaxByForm({
                        url: `/file/upload/` + util.getUrlParam(document.location.href, 'caseId'),
                        form: form,
                        success: function (data) {
                            _self.$emit('getFileList', data.message)
                        },
                        error: function (err) {
                            alert('上传失败, 请重试');
                        }
                    });
                }*/
            },
            query: function () {
                const _self = this;
                let element = document.getElementById("uploadText3");
                if (element.value !== "-1") {
                    _self.returnValue(element.value);
                } else {
                    setTimeout(function () {
                        _self.query();
                    }, 1000);
                }
            },
            returnValue: function (url) {
                const _self = this;
                let form = new FormData();
                form.append("url", url);
                util.ajaxByForm({
                    url: `/file/uploadByIcbcUrl`,
                    form: form,
                    success: function (data) {
                        _self.$emit("getFileList", data.data);
                        document.getElementById("uploadText3").value = "-1";
                        _self.query();
                    },
                    error: function (err) {
                        alert('上传失败, 请重试');
                        document.getElementById("uploadText3").value = "-1";
                        _self.query();
                    }
                });
            }
        }
    };
</script>

<style>
    .file-input-button {
        text-align: left;
        position: relative;
        display: inline-block;
        overflow: hidden;
    }

    .file-input-button input {
        position: absolute;
        right: 0;
        top: 0;
        opacity: 0;
        -ms-filter: 'alpha(opacity=0)';
        font-size: 200px;
    }
</style>
